#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 110;

ll f[N][N][N],a[N];
char s[N];
int n;

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in;	scanf("%s",s+1); 
	for(int i = 1;i <= n;i++) a[i] = in;
	for(int len = 1;len <= n;len++){
		for(int l = 1,r = len;r <= n;l++,r++){
			for(int i = 0;i <= n-r;i++) f[l][r][i] = max(f[l][r][i],f[l][r-1][0] + a[i+1]);
			for(int k = l;k < r;k++)
				if(s[k] == s[r])
					for(int i = 0;i <= n-r;i++)
						f[l][r][i] = max(f[l][r][i],f[l][k][i+1] + f[k+1][r-1][0]);
		}
	}
	printf("%lld\n",f[1][n][0]);
	return 0;
}

